#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>
#include <WinAPIEx.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>


Global Const $n_color = 0xe0e0e0
Global Const $s_color = 0x909090

Global Const $row_length = 6
Global Const $col_length = 6
Global Const $place_width = 20
Global Const $place_height = 20
Global Const $btn_width = 60
Global Const $btn_height = 20
Global Const $adv_max = 20

Global Const $stat_width = 60

Global Const $advb_width = 60
Global Const $adv_width = 430
Global Const $adv_height = 20
Global Const $gui_width = $advb_width + $adv_width
Global Const $lbl_width = $gui_width - $row_length * $place_width - $stat_width
Global Const $gui_height = $col_length * $place_height + $adv_max * $adv_height + $btn_height

Global Const $lbl_height = $col_length * $place_height
Global Const $stat_height = $lbl_height


Global $hGUI
Global $label
Global $map_stat
Global $btn_map[$row_length][$col_length]
Global $btn_act[5][3] = [ _
	["read", "read_field", False], _
	["save map", "make_advice", False], _
	["load", "load_map", False], _
	["calc", "gen_advice", False], _
	["save ext", "make_advice_l", False] _
]
;	["test", "random_field_clear", False], _
;	["rand", "random_field", False], _
;	["calc l", "make_calculate_log", False], _
;	["calc", "make_calculate", False], _

Global $btn_adv[$adv_max]
Global $lbl_adv[$adv_max]
Global $btn_adv_app[$adv_max]

#include "str.au3"
#include "field.au3"
#include "choose.au3"
#include "action.au3"
#include "advice.au3"
#include "compare.au3"
#include "capture.au3"
#include "forecast.au3"

Func _main()
   create_gui()
   main_loop()
EndFunc


Func create_gui()
	$hGUI = GUICreate("helper", $gui_width, $gui_height, 0, 0)
	$label = GUICtrlCreateLabel("label", _
		$row_length * $place_width + $stat_width, _
		0, _
		$lbl_width, _
		$lbl_height)
	Local $sr = $row_length * $place_width
	Local $st = 0
	Local $sw = $stat_width
	Local $sh = $stat_height
	$map_stat = GUICtrlCreateLabel("stat", $sr, $st, $sw, $sh, BitOR($SS_ETCHEDFRAME,$SS_LEFT))
	$map_stat = GUICtrlCreateLabel("stat", $sr+2, $st+2, $sw-4, $sh-4)
	GUICtrlSetFont($map_stat, 8.5, 400, 0, "Courier New", 5)
	For $r = 0 To $row_length - 1
		For $c = 0 To $col_length - 1
			$btn_map[$r][$c] = GUICtrlCreateButton($r & $c,$c * $place_width,$r * $place_height,$place_width,$place_width)
			set_field_state($r,$c)
		Next
	Next
	For $b = 0 To Ubound($btn_act,1) - 1
		$btn_act[$b][2] = GUICtrlCreateButton($btn_act[$b][0], $b * $btn_width, $row_length * $place_height,$btn_width,$btn_height)
	Next
	Local $shift = $row_length * $place_height + $btn_height
	For $a = 0 To $adv_max - 1
		$lbl_adv[$a] = GUICtrlCreateLabel("", $advb_width, $shift + $a * $adv_height,$adv_width,$adv_height)
		GUICtrlSetFont($lbl_adv[$a], 9.5, 400, 0, "Courier New", 5)
	Next
	For $a = 0 To $adv_max - 1
		$btn_adv[$a] = GUICtrlCreateButton("", 0, $shift + $a * $adv_height,$advb_width,$adv_height)
	Next
	;For $a = 0 To $adv_max - 1
	;	$btn_adv_app[$a] = GUICtrlCreateButton("", $advb_width + $adv_width, $shift + $a * $adv_height,$advb_width,$adv_height)
	;Next
	GUISetState(@SW_SHOW,$hGUI)
	WinActivate($hGUI)
EndFunc

Func main_loop()
	Local _
		$f_act = $btn_act[0][2], _
		$l_act = $btn_act[UBound($btn_act,1) - 1][2], _
		$f_adv = $btn_adv[0], _
		$l_adv = $btn_adv[UBound($btn_adv,1) - 1];, _
		;$f_app = $btn_adv_app[0], _
		;$l_app = $btn_adv_app[UBound($btn_adv_app,1) - 1]
	While True
		Local $msg = GUIGetMsg()
		Local $n_act = UBound($btn_act,1)
		Switch $msg
			Case $GUI_EVENT_CLOSE
			   ExitLoop
			Case $f_act To $l_act
				process_action($msg - $f_act)
			Case $f_adv To $l_adv
				process_adv($msg - $f_adv)
			;Case $f_app To $l_app
			;	process_adv_app($msg - $f_app)
		EndSwitch
	WEnd
EndFunc

Func set_advice($n, $type, $msg = "")
	GUICtrlSetData($btn_adv[$n],$type)
	GUICtrlSetData($lbl_adv[$n],$msg)
	If $msg <> "" Then
		GUICtrlSetData($btn_adv_app[$n],"apply")
	Else
		GUICtrlSetData($btn_adv_app[$n],"")
	EndIF
EndFunc

Func set_field($r,$c,$msg)
	GUICtrlSetData($btn_map[$r][$c],$msg)
EndFunc

Func set_field_state($r,$c,$state = False)
	Local $color
	If $state Then
		$color = $s_color
	Else
		$color = $n_color
	EndIF
	GUICtrlSetBkColor($btn_map[$r][$c],$color)
EndFunc

Func set_label($msg)
	GUICtrlSetData($label,$msg)
EndFunc

Func set_stat($msg)
	GUICtrlSetData($map_stat,$msg)
EndFunc

Func process_action($n)
	Call($btn_act[$n][1])
EndFunc

Func process_adv($n)
	show_advice($n)
EndFunc

Func process_adv_app($n)
	apply_advice($n)
EndFunc

